package com.zyb.beiojmodel.model.vo;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;

import com.zyb.beiojmodel.model.dto.question.QuestionJudgeConfig;
import com.zyb.beiojmodel.model.entity.Question;
import lombok.Data;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 题目视图  （脱敏）
 *
 * @author <a href="https://github.com/lizyb">程序员鱼皮</a>
 * @from <a href="https://zyb.icu">编程导航知识星球</a>
 */
@Data
public class QuestionVO implements Serializable {

    private static final long serialVersionUID = -7442629325499374481L;
    /**
     * id
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * 标签列表（json 数组）
     */
    private List<String> tags;

    /**
     * 题目提交数
     */
    private Integer submitNum;

    /**
     * 题目通过数
     */
    private Integer acceptedNum;


    /**
     * 判题配置（json 对象）
     */
    private QuestionJudgeConfig judgeConfig;

    /**
     * 点赞数
     */
    private Integer thumbNum;

    /**
     * 收藏数
     */
    private Integer favourNum;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 题目的出题人信息
     */
    private UserVO userVO;


    /**
     * 包装类转对象
     *
     * @param questionVO
     * @return
     */
    public static Question voToObj(QuestionVO questionVO) {
        if (questionVO == null) {
            return null;
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionVO, question);
        QuestionJudgeConfig questionJudgeConfig = questionVO.getJudgeConfig();
        if (ObjectUtil.isNotNull(questionJudgeConfig)) {
            question.setJudgeConfig(JSONUtil.toJsonStr(questionJudgeConfig));
        }
        List<String> questionVOTags = questionVO.getTags();
        if (questionVOTags != null) {
            question.setTags(JSONUtil.toJsonStr(questionVOTags));
        }

        return question;
    }

    /**
     * 对象转包装类
     *
     * @param question
     * @return
     */
    public static QuestionVO objToVo(Question question) {
        if (question == null) {
            return null;
        }
        QuestionVO questionVO = new QuestionVO();
        BeanUtils.copyProperties(question, questionVO);
        String strConfig = question.getJudgeConfig();
        if (StrUtil.isNotBlank(strConfig)) {
            questionVO.setJudgeConfig(JSONUtil.toBean(strConfig,QuestionJudgeConfig.class));
        }
        String strTags = question.getTags();
        if (StrUtil.isNotBlank(strTags)) {
            questionVO.setTags(JSONUtil.toList(strTags,String.class));
        }
        return questionVO;
    }
}
